package com.bdqn.crm.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.crm.config.UtilsConfig;
import com.bdqn.crm.pojo.*;
import com.bdqn.crm.service.BasDictService;
import com.bdqn.crm.service.CstCustomerService;
import com.bdqn.crm.service.CstServiceService;
import com.bdqn.crm.service.SysUserService;
import com.bdqn.crm.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/service")
public class CstServiceController {

    @Resource
    private BasDictService basDictService;

    @Resource
    private CstServiceService cstServiceService;

    @Resource
    private CstCustomerService cstCustomerService;

    @Resource
    private SysUserService sysUserService;


    @RequestMapping("/add")
    public String toadd(Model model){
        List<CstCustomer> customers = cstCustomerService.list();
        QueryWrapper<BasDict> qw = new QueryWrapper<>();
        qw.eq("dict_type","服务类型");
        List<BasDict> dicts = basDictService.list(qw);
        model.addAttribute("dicts",dicts);
        model.addAttribute("customers",customers);
        return "service/add";
    }

    @RequestMapping("/save")
    public String save(HttpServletRequest request, CstService cstService){
        SysUser creteUser = (SysUser) request.getSession().getAttribute(UtilsConfig.SysUserLoginName);
        cstService.setSvrCreateId(creteUser.getUsrId());
        cstService.setSvrCreateBy(creteUser.getUsrName());
        cstService.setSvrDueDate(new Date());
        cstService.setSvrDealDate(new Date());
        if (cstServiceService.saveOrUpdate(cstService)) {
            return "redirect:/service/dispatch";
        }
        return "service/add";
    }

    @RequestMapping("/dispatch")
    public String dispatch(HttpServletRequest request, Model model, @RequestParam(value = "svrCustName",required = false) String svrCustName, @RequestParam(value = "svrTitle",required = false) String svrTitle, @RequestParam(value = "svrType",required = false) String svrType,@RequestParam(required = false, defaultValue = "1") int pageIndex){
        Page<CstService> page = new Page<>(pageIndex, 5);
        QueryWrapper<CstService> queryWrapper = new QueryWrapper<>();
        //判断是否有查询条件
        if (svrCustName != null && svrCustName.trim() != "") {
            queryWrapper.like("svr_cust_name", svrCustName);      //模糊设置条件
        }
        if (svrTitle != null && svrTitle.trim() != "") {
            queryWrapper.like("svr_title", svrTitle);
        }
        if (svrType != null && svrType.trim() != "") {
            queryWrapper.like("svr_type", svrType);
        }
        if (cstServiceService.count(queryWrapper) == 0) {     //此操作说明 数据库没有数据可展示，那么就全部清零
            page.setTotalElements(0);
            page.setContent(null);
        } else {
            page.setTotalElements(cstServiceService.count(queryWrapper));
            page.setContent(cstServiceService.selectPages(svrCustName, svrTitle,svrType ,(page.getNumber() - 1) * page.getPageSize(), page.getPageSize()));
        }

        //服务类型
        QueryWrapper<BasDict> basDictQueryWrapper = new QueryWrapper<>();
        basDictQueryWrapper.eq("dict_type","服务类型");
        List<BasDict> basDicts = basDictService.list(basDictQueryWrapper);

        //users
        List<SysUser> users = sysUserService.list();

        model.addAttribute("users",users);
        model.addAttribute("dicts",basDicts);
        model.addAttribute("svrCustName",svrCustName);
        model.addAttribute("svrTitle",svrTitle);
        model.addAttribute("svrType",svrType);
        model.addAttribute("servicePage", page);
        return "service/dispatch";
    }

    @ResponseBody
    @GetMapping("/allot/{svrId}/{svrDueId}")
    public String allot(Map<String,String> map, @PathVariable("svrId") Integer svrId, @PathVariable("svrDueId") Integer svrDueId ){
        SysUser sysUser = sysUserService.getById(svrDueId);
        CstService cstService = cstServiceService.getById(svrId);
        cstService.setSvrDueId(sysUser.getUsrId());
        cstService.setSvrDueTo(sysUser.getUsrName());
        cstService.setSvrDueDate(new Date());
        cstService.setSvrDealDate(new Date());
        if (cstServiceService.saveOrUpdate(cstService)) {
            map.put("editResult","true");
        }
        return JSON.toJSONString(map);
    }

    @ResponseBody
    @GetMapping("del/{svrId}")
    public String del(Map<String,String> map,@PathVariable("svrId") Integer svrId){
        if (cstServiceService.removeById(svrId)) {
            map.put("delResult","true");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("deal")
    public String deal(HttpServletRequest request, Model model, @RequestParam(value = "svrCustName",required = false) String svrCustName, @RequestParam(value = "svrTitle",required = false) String svrTitle, @RequestParam(value = "svrType",required = false) String svrType,@RequestParam(required = false, defaultValue = "1") int pageIndex){
        Page<CstService> page = new Page<>(pageIndex, 5);
        QueryWrapper<CstService> queryWrapper = new QueryWrapper<>();
        //判断是否有查询条件
        if (svrCustName != null && svrCustName.trim() != "") {
            queryWrapper.like("svr_cust_name", svrCustName);      //模糊设置条件
        }
        if (svrTitle != null && svrTitle.trim() != "") {
            queryWrapper.like("svr_title", svrTitle);
        }
        if (svrType != null && svrType.trim() != "") {
            queryWrapper.like("svr_type", svrType);
        }
        if (cstServiceService.count(queryWrapper) == 0) {     //此操作说明 数据库没有数据可展示，那么就全部清零
            page.setTotalElements(0);
            page.setContent(null);
        } else {
            page.setTotalElements(cstServiceService.count(queryWrapper));
            page.setContent(cstServiceService.selectPages(svrCustName, svrTitle,svrType ,(page.getNumber() - 1) * page.getPageSize(), page.getPageSize()));
        }

        //服务类型
        QueryWrapper<BasDict> basDictQueryWrapper = new QueryWrapper<>();
        basDictQueryWrapper.eq("dict_type","服务类型");
        List<BasDict> basDicts = basDictService.list(basDictQueryWrapper);

        //users
        List<SysUser> users = sysUserService.list();

        model.addAttribute("users",users);
        model.addAttribute("dicts",basDicts);
        model.addAttribute("svrCustName",svrCustName);
        model.addAttribute("svrTitle",svrTitle);
        model.addAttribute("svrType",svrType);
        model.addAttribute("page", page);
        return "service/deal";
    }


    @RequestMapping("edit")
    public String edit(Model model,@RequestParam("svrId") Integer svrId){
        CstService service = cstServiceService.getById(svrId);
        System.out.println(JSON.toJSONString(service));
        model.addAttribute("serviced",service);
        return "service/dealEdit";
    }

    @RequestMapping("editDeal")
    public String editDeal(Model model,CstService cstService){
        if (cstServiceService.saveOrUpdate(cstService)) {
            return "redirect:/service/deal";
        }
        model.addAttribute("serviced",cstService);
        return "service/dealEdit";
    }

    @RequestMapping("feedback")
    public String feedback(HttpServletRequest request, Model model, @RequestParam(value = "svrCustName",required = false) String svrCustName, @RequestParam(value = "svrTitle",required = false) String svrTitle, @RequestParam(value = "svrType",required = false) String svrType,@RequestParam(required = false, defaultValue = "1") int pageIndex){
        Page<CstService> page = new Page<>(pageIndex, 5);
        QueryWrapper<CstService> queryWrapper = new QueryWrapper<>();
        //判断是否有查询条件
        if (svrCustName != null && svrCustName.trim() != "") {
            queryWrapper.like("svr_cust_name", svrCustName);      //模糊设置条件
        }
        if (svrTitle != null && svrTitle.trim() != "") {
            queryWrapper.like("svr_title", svrTitle);
        }
        if (svrType != null && svrType.trim() != "") {
            queryWrapper.like("svr_type", svrType);
        }
        if (cstServiceService.count(queryWrapper) == 0) {     //此操作说明 数据库没有数据可展示，那么就全部清零
            page.setTotalElements(0);
            page.setContent(null);
        } else {
            page.setTotalElements(cstServiceService.count(queryWrapper));
            page.setContent(cstServiceService.selectPages(svrCustName, svrTitle,svrType ,(page.getNumber() - 1) * page.getPageSize(), page.getPageSize()));
        }

        //服务类型
        QueryWrapper<BasDict> basDictQueryWrapper = new QueryWrapper<>();
        basDictQueryWrapper.eq("dict_type","服务类型");
        List<BasDict> basDicts = basDictService.list(basDictQueryWrapper);

        //users
        List<SysUser> users = sysUserService.list();

        model.addAttribute("users",users);
        model.addAttribute("dicts",basDicts);
        model.addAttribute("svrCustName",svrCustName);
        model.addAttribute("svrTitle",svrTitle);
        model.addAttribute("svrType",svrType);
        model.addAttribute("feedpage", page);
        return "service/feedback";
    }

    @RequestMapping("/toFeedBack")
    public String toFeedBack(Model model,Integer svrId){
        CstService byId = cstServiceService.getById(svrId);
        model.addAttribute("serviced",byId);
        return "service/feedbackEdit";
    }

    @RequestMapping("editFB")
    public String editFB(Model model,CstService cstService){
        if (cstServiceService.saveOrUpdate(cstService)) {
            return "redirect:/service/feedback";
        }
        model.addAttribute("serviced",cstService);
        return "service/feedbackEdit";
    }

    @RequestMapping("arch")
    public String toArch(HttpServletRequest request, Model model, @RequestParam(value = "svrCustName",required = false) String svrCustName, @RequestParam(value = "svrTitle",required = false) String svrTitle, @RequestParam(value = "svrType",required = false) String svrType,@RequestParam(required = false, defaultValue = "1") int pageIndex){
        Page<CstService> page = new Page<>(pageIndex, 5);
        QueryWrapper<CstService> queryWrapper = new QueryWrapper<>();
        //判断是否有查询条件
        if (svrCustName != null && svrCustName.trim() != "") {
            queryWrapper.like("svr_cust_name", svrCustName);      //模糊设置条件
        }
        if (svrTitle != null && svrTitle.trim() != "") {
            queryWrapper.like("svr_title", svrTitle);
        }
        if (svrType != null && svrType != "") {
            queryWrapper.like("svr_type", svrType);
        }
        if (cstServiceService.count(queryWrapper) == 0) {     //此操作说明 数据库没有数据可展示，那么就全部清零
            page.setTotalElements(0);
            page.setContent(null);
        } else {
            page.setTotalElements(cstServiceService.count(queryWrapper));
            page.setContent(cstServiceService.selectPages(svrCustName, svrTitle,svrType ,(page.getNumber() - 1) * page.getPageSize(), page.getPageSize()));
        }

        //服务类型
        QueryWrapper<BasDict> basDictQueryWrapper = new QueryWrapper<>();
        basDictQueryWrapper.eq("dict_type","服务类型");
        List<BasDict> basDicts = basDictService.list(basDictQueryWrapper);

        //users
        List<SysUser> users = sysUserService.list();

        model.addAttribute("users",users);
        model.addAttribute("dicts",basDicts);
        model.addAttribute("svrCustName",svrCustName);
        model.addAttribute("svrTitle",svrTitle);
        model.addAttribute("svrType",svrType);
        model.addAttribute("servicePage", page);
        return "service/arch";
    }

    @RequestMapping("findArch")
    public String findArch(Model model,Integer svrId){
        CstService cstService = cstServiceService.getById(svrId);
        model.addAttribute("serviced",cstService);
        return "service/archInfo";
    }

}
